//
//  WarnDetailsViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/9/2.
//

#import "WarnDetailsViewController.h"
#import "WarnModel.h"
#import "WarnListDetailsTableViewCell.h"
#import "WarnDetailsFooterView.h"
#import "DescriptionShowView.h"
#import "getPondTissueListModel.h"
#import "YTSelectStringShowView.h"
#import "TransferTaskViewController.h"
#import "CancelTaskViewController.h"
#import "foundQuestViewController.h"



@interface WarnDetailsViewController ()
@property (nonatomic ,strong) WarnModel * model;
@property (nonatomic ,strong) UIView * buttomview;
@property (nonatomic ,strong) UIButton * xzrwBtn;
@property (nonatomic ,strong) UIButton * gbgjBtn;
@property (nonatomic ,strong) UIButton * qxrwBtn;
@property (nonatomic ,strong) UIButton * zjrwBtn;
@property (nonatomic ,strong) UIButton * lxygBtn;

@end

@implementation WarnDetailsViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(self.model){
        [self loadNewData];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(170));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"告警详情" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    UIView * buttomview = [UIView new];
    buttomview.clipsToBounds = YES;
    self.buttomview = buttomview;
    buttomview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:buttomview];
    [buttomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(63)+ KSafeAreaBottomHeight);
    }];
    
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#E6E9EE");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
        make.bottom.equalTo(buttomview.mas_top);
    }];
    
    [self loadNewData];
}

- (void)loadNewData {
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
//    self.ID = @"616464618";
    [dict setObject:self.ID forKey:@"id"];
    [SCNetManger PostAlarmManageAlarmManageDetailWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            self.model = [WarnModel yy_modelWithDictionary:model.result];
            if(self.model.status == 0){
                [self.buttomview mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(kwt(63)+ KSafeAreaBottomHeight);
                }];
                [self addBtn];
            } else {
                [self.buttomview mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(0);
                }];
            }
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

- (void)addBtn{
    [self.buttomview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(63)+ KSafeAreaBottomHeight);
    }];
    
    [self.buttomview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    ///告警类型 1.池塘异常、2.任务异常、3.仓储异常、4.天气异常
    if(self.model.alarmType == 1){///池塘异常
        [self.buttomview addSubview:self.xzrwBtn];
        [self.xzrwBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(kwt(93));
            make.height.offset(kwt(36));
            make.right.offset(kwt(-12));
            make.top.offset(kwt(11));
        }];
        if(self.model.canClose){
            [self.buttomview addSubview:self.gbgjBtn];
            [self.gbgjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(kwt(93));
                make.height.offset(kwt(36));
                make.right.equalTo(self.xzrwBtn.mas_left).offset(kwt(-14));
                make.top.offset(kwt(11));
            }];
        }
      
        
    } else if(self.model.alarmType == 2){///任务异常
        [self.buttomview addSubview:self.lxygBtn];
        [self.lxygBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(kwt(93));
            make.height.offset(kwt(36));
            make.right.offset(kwt(-12));
            make.top.offset(kwt(11));
        }];
        [self.buttomview addSubview:self.zjrwBtn];
        [self.zjrwBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(kwt(93));
            make.height.offset(kwt(36));
            make.right.equalTo(self.lxygBtn.mas_left).offset(kwt(-14));
            make.top.offset(kwt(11));
        }];
        [self.buttomview addSubview:self.qxrwBtn];
        [self.qxrwBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(kwt(93));
            make.height.offset(kwt(36));
            make.right.equalTo(self.zjrwBtn.mas_left).offset(kwt(-14));
            make.top.offset(kwt(11));
        }];
    } else if(self.model.alarmType == 3){///仓储异常
        if(self.model.canClose){
            [self.buttomview addSubview:self.gbgjBtn];
            [self.gbgjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(kwt(93));
                make.height.offset(kwt(36));
                make.right.offset(kwt(-12));
                make.top.offset(kwt(11));
            }];
        } else {
            [self.buttomview mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(0);
            }];
        }
    } else if(self.model.alarmType == 4){///天气异常
        if(self.model.canClose){
            [self.buttomview addSubview:self.gbgjBtn];
            [self.gbgjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(kwt(93));
                make.height.offset(kwt(36));
                make.right.offset(kwt(-12));
                make.top.offset(kwt(11));
            }];
        } else {
            [self.buttomview mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(0);
            }];
            
        }
    } else if(self.model.alarmType == 5){///只有联系员工
        [self.buttomview addSubview:self.lxygBtn];
        [self.lxygBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(kwt(93));
            make.height.offset(kwt(36));
            make.right.offset(kwt(-12));
            make.top.offset(kwt(11));
        }];
    }
    
    
}
- (UIButton *)xzrwBtn{
    if(!_xzrwBtn){
        _xzrwBtn = [UIButton buttonWithType:0];
        _xzrwBtn.addNormalText(@"新增任务").addNormalColor(UIColor.whiteColor).addFont(kCFont(16)).addTarget(self,@selector(xzrwbtnclick)).addbackgroundColor([UIColor gradientColorWithSize:CGSizeMake(kwt(93), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")]);
        [DTKTool YJ:kwt(5) andView:_xzrwBtn];
    }
    return _xzrwBtn;
}
- (void)xzrwbtnclick{
    foundQuestViewController * vc = [foundQuestViewController new];
    vc.alarmId = self.model.ID;
    vc.pondid = self.model.pondId;
    [self.navigationController pushViewController:vc animated:YES];

    
}
- (UIButton *)gbgjBtn{
    if(!_gbgjBtn){
        _gbgjBtn = [UIButton buttonWithType:0];
        _gbgjBtn.addNormalText(@"关闭告警").addNormalColor(UIColor.whiteColor).addFont(kCFont(16)).addTarget(self,@selector(GBBtnClick)).addbackgroundColor([UIColor gradientColorWithSize:CGSizeMake(kwt(93), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#FC734A") endColor:KcolorHex(@"#F74733")]);
        [DTKTool YJ:kwt(5) andView:_gbgjBtn];
    }
    return _gbgjBtn;
}
- (void)GBBtnClick{
    ///审批拒绝
    MJWeakSelf
    [DescriptionShowView showViewWithTitle:@"关闭告警" andContent:@"关闭原因" andBlock:^(NSString * _Nonnull str) {
        [self.view showBusyHUD];
        NSMutableDictionary * dict = [NSMutableDictionary dictionary];
        [dict setObject:weakSelf.model.ID forKey:@"id"];
        [dict setObject:str forKey:@"reason"];
        [SCNetManger PostAlarmManageCloseWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
            [self.view hideBusyHUD];
            if(model.code == 200){
                [self.view showWarning:model.message];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    // 2秒后执行的代码
                    [self loadNewData];
                });
            } else {
                [self.view showWarning:model.message];
            }
        }];
    }];;
}
- (UIButton *)lxygBtn{
    if(!_lxygBtn){
        _lxygBtn = [UIButton buttonWithType:0];
        _lxygBtn.addNormalText(@"联系员工").addNormalColor(UIColor.whiteColor).addFont(kCFont(16)).addTarget(self,@selector(lxygBtnclick)).addbackgroundColor([UIColor gradientColorWithSize:CGSizeMake(kwt(93), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")]);
        [DTKTool YJ:kwt(5) andView:_lxygBtn];
    }
    return _lxygBtn;
}
- (void)lxygBtnclick{
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.model.alarmData.executorId forKey:@"id"];
    [SCNetManger PostAlarmManageGetPhoneByUserIdsWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray <PondUserModel *> * array = [NSArray yy_modelArrayWithClass:PondUserModel.class json:model.result];
            if(array.count < 1){
                [self.view showWarning:@"无法获取号码"];
            } else if(array.count == 1){
                  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",array.firstObject.phone]] options:nil completionHandler:nil];
            }else{
                NSMutableArray * nameArray = [NSMutableArray array];
                for (PondUserModel * pmodel in array) {
                    [nameArray addObject:pmodel.realName];
                }
                [YTSelectStringShowView showViewWithTitle:@"选择员工" andIndex:0 andDateArray:nameArray andBlock:^(NSString * _Nonnull str) {
                    for (PondUserModel * pmodel in array) {
                        if([str isEqualToString:pmodel.realName]){
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",pmodel.phone]] options:nil completionHandler:nil];
                            return;
                        }
                    }
                }];
            }
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

- (UIButton *)zjrwBtn{
    if(!_zjrwBtn){
        _zjrwBtn = [UIButton buttonWithType:0];
        _zjrwBtn.addNormalText(@"转交").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(16)).addTarget(self,@selector(openJJ)).addbackgroundColor(UIColor.whiteColor);
        [DTKTool YJBK:kwt(5) andView:_zjrwBtn andWidth:0.5 andColor:KcolorHex(@"#CDCDCD")];
    }
    return _zjrwBtn;
}
- (UIButton *)qxrwBtn{
    if(!_qxrwBtn){
        _qxrwBtn = [UIButton buttonWithType:0];
        _qxrwBtn.addNormalText(@"取消").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(16)).addTarget(self,@selector(openQX)).addbackgroundColor(UIColor.whiteColor);
        [DTKTool YJBK:kwt(5) andView:_qxrwBtn andWidth:0.5 andColor:KcolorHex(@"#CDCDCD")];
    }
    return _qxrwBtn;
}

///交接任务
- (void)openJJ{
    TransferTaskViewController * vc = [TransferTaskViewController new];
    vc.ID = self.model.taskId;
    vc.type = 1;
    [self.navigationController pushViewController:vc animated:YES];
}
///取消任务
- (void)openQX{
    CancelTaskViewController * vc = [CancelTaskViewController new];
    vc.ID = self.model.taskId;
    [self.navigationController pushViewController:vc animated:YES];
}


//- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
//    return kwt(100);
//}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    WarnDetailsFooterView * footerview = [WarnDetailsFooterView NewView];
    footerview.model = self.model;
    MJWeakSelf
    footerview.openReloadBlock = ^{
        [weakSelf.tableview reloadData];
    };
    return footerview;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return self.model.FooterviewH;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.model.DetailsCellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    
    WarnListDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"WarnListDetailsTableViewCell"];
    if (cell == nil) {
        cell = [[WarnListDetailsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WarnListDetailsTableViewCell"];
    }
    cell.model = self.model;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell ;
    
}



@end
